Page({

  /**
   * 页面的初始数据
   */
  data: {
    allOrderList:[], //用来存储指定日期内所有订单记录
    orderList: [], //筛选后的订单数据
    //---------订单筛选的几个条件
    status: [
      { name: "所有状态", count: 0},
      { name: "进行中", count: 0 },
      { name: "已完成", count: 0 },
    ], //1.订单状态
    orderDate: '',//2.订单时间
    eatWay: [
      { name: "所有方式", count: 0 },
      { name: "在店即食", count: 0 },
      { name: "预约到店", count: 0 },
      { name: "外卖配送", count: 0 },
    ],//3.就餐类型
    //---------订单条件的返回值
    eatWayId: 0, //就餐方式当前所选id
    statusId: 0 //订单状态当前所选id 
  },
  //******************************************* 更改【点击事件】
  dateChange: function (e) {
    console.log(e.detail.value)
    this.setData({
      orderDate: e.detail.value
    })
    //当改变日期的时候，需要重新request数据库
    this.getOrderList();
  },
  //*******************************************  就餐方式【点击事件】
  eatWayClick: function (e) {
    let id = e.currentTarget.dataset.id;
    this.setData({
      eatWayId: id
    })
    this.constructList();
  },
  //*******************************************  订单状态【点击事件】
  statusClick: function (e) {
    let id = e.currentTarget.dataset.id;
    this.setData({
      statusId: id
    })
    this.constructList();
  },
  //******************************************* 动态生成列表
  constructList: function () {
    let allOrderList = this.data.allOrderList;
    let eatWay = this.data.eatWay;
    let status = this.data.status;
    let eatWayId = this.data.eatWayId;
    let statusId = this.data.statusId;
    let orderList=[];
    for (let i = 0; i < allOrderList.length;i++)
    {
      if (eatWayId == 0 || allOrderList[i].orderTypeName == eatWay[eatWayId].name) //就餐方式满足
      {
        if (statusId == 0 || allOrderList[i].status == status[statusId].name) //订单状态满足
        {
          orderList.push(allOrderList[i]);
        }
      }
    }
    this.setData({
        orderList: orderList
      })
  },
  //*******************************************  根据条件查询订单页面的数据
  getOrderList: function () {
    let that = this;
    //----------------
    wx.showLoading({
      title: '正在加载'
    })
    wx.cloud.callFunction({
      name:'manageOrders',
      data: { orderDate: that.data.orderDate}
    })
    .then(res=>{
      wx.hideLoading()
      console.log(res.result)
      let allOrderList = res.result;
      //根据allOrderList计算各种类型订单的数量
      that.calculateOrderNumber(allOrderList);
      //针对用户当前所选择的筛选设置，进行订单视图刷新
      that.data.allOrderList = allOrderList; //存储所有订单记录
      that.constructList();
    })
    .catch(err=>{
      wx.hideLoading()
      wx.showModal({
        title: '友情提示',
        content: '查询订单失败:'+err,
        showCancel: false
      })
      wx.navigateBack();
    })
  },
  //*******************************************  根据订单记录数组【计算各类别订单数量】
  calculateOrderNumber:function(orders){
    let inProcess = 0;//进行中count
    let finish = 0; //已完成count
    let inshop = 0; //在店即食
    let reserve = 0; //预约到店
    let deliver = 0; //外卖配送
    for (let i = 0; i < orders.length; i++) {
      if (orders[i].status == '进行中') {
        inProcess++;
      }
      if (orders[i].status == '已完成') {
        finish++;
      }
      if (orders[i].orderTypeName == '在店即食') {
        inshop++;
      }
      if (orders[i].orderTypeName == '预约到店') {
        reserve++;
      }
      if (orders[i].orderTypeName == '外卖配送') {
        deliver++;
      }
    }
    this.setData({
      'status[0].count': orders.length,
      'status[1].count': inProcess,
      'status[2].count': finish,
      'eatWay[0].count': orders.length,
      'eatWay[1].count': inshop,
      'eatWay[2].count': reserve,
      'eatWay[3].count': deliver,
    })
  },
  //*******************************************  用户点击某单后，查看订单详情
  checkOrderDetail: function (e) {
    let that = this;
    let orderId = e.currentTarget.dataset.id;
    //console.log(orderId)
    wx.showLoading({ title: '正在查询' })
    const db = wx.cloud.database();
    db.collection('orders').where({ orderId: orderId }).get()
      .then(res => {
        getApp().globalData.orderDetail = res.data[0];
        wx.navigateTo({
          url: '../../pages/orderDetail/orderDetail'
        })
        wx.hideLoading();
      })
      .catch(err => {
        wx.hideLoading();
        wx.showModal({
          title: '友情提示',
          content: '读取订单信息出错:' + err,
          showCancel: false
        })
      })
  },
  //*******************************************  保存【订单时间\就餐方式\订单状态】至内存
  recordQuerySettings:function(){
      let querySettings={};
      querySettings.orderDate=this.data.orderDate;
      querySettings.eatWayId = this.data.eatWayId;
      querySettings.statusId = this.data.statusId;
    wx.setStorageSync('querySettings', querySettings);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //每次访问页面，也尝试从内存中获取上次查询的设置，如果内存中没有记录，则赋初始值
    let querySettings = wx.getStorageSync('querySettings');
    if (querySettings)
    {
      //如果内存中有querySettings的定义，则同步设置
      this.setData({
        orderDate: querySettings.orderDate,
        eatWayId: querySettings.eatWayId,
        statusId: querySettings.statusId
      })
    }
    else
    {
      //获取今日的日期，并且赋值到data中的orderDate
      this.getNowFormatDate();
      //eatWayId，statusId两个id默认为0
    }

    //根据上述的订单日期，查询该日期内的所有订单记录
    this.getOrderList();
  },

  //获取当前时间，格式YYYY-MM-DD
  getNowFormatDate: function () {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    this.setData({
      orderDate: currentdate
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.recordQuerySettings();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.recordQuerySettings();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})